package com.cqtczh.qhd.driver.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javabeen.PushMessage;



import com.baidu.android.pushservice.PushMessageReceiver;
import com.cqtczh.qhd.driver.AppContent;
import com.cqtczh.qhd.driver.acticity.delet_carteam_activity;
import com.cqtczh.qhd.driver.acticity.order.OrderDetails;
import com.cqtczh.qhd.driver.api.GagApi;
import com.google.gson.Gson;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

/**
 * * @author 作者 陈修园:
 * 
 * @date 创建时间：2015-4-16 下午1:30:56
 * @version 1.0
 * @parameter 用来推送消息接收者
 * @since
 * @return
 */
public class CEPushMessageReceiver extends PushMessageReceiver {

	/** TAG to Log */
	public static final String TAG = CEPushMessageReceiver.class.getSimpleName();

	@Override
	public void onBind(Context context, int errorCode, String appid,
			String userId, String channelId, String requestId) {
		// TODO Auto-generated method stub
		// Log.v(, "绑定成功");
		String responseString = "onBind errorCode=" + errorCode + " appid="
				+ appid + " userId=" + userId + " channelId=" + channelId
				+ " requestId=" + requestId;
		Log.d(TAG, "绑定成功+" + responseString);
		GagApi.channelId = channelId;
		AppContent app;
		app = (AppContent) context.getApplicationContext();
		app.channelId = channelId;
	//	app.messagepushuserId = userId;
		
	}

	@Override
	public void onDelTags(Context arg0, int arg1, List<String> arg2,
			List<String> arg3, String arg4) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onListTags(Context arg0, int arg1, List<String> arg2,
			String arg3) {
		// TODO Auto-generated method stub

	}

	/**
	 * 接收透传消息的函数。
	 * 
	 * @param context
	 *            上下文
	 * @param message
	 *            推送的消息
	 * @param customContentString
	 *            自定义内容,为空或者json字符串
	 */
	@Override
	public void onMessage(Context context, String message,
			String customContentString) {
		// TODO Auto-generated method stub
		String messageString = "透传消息 message=\"" + message
				+ "\" customContentString=" + customContentString;
		Log.d(TAG, messageString);
		// if (message.equals("100110")) {
		updateContent(context, message);
		// }
	}

	@Override
	public void onNotificationClicked(Context arg0, String arg1, String arg2,
			String arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onSetTags(Context arg0, int arg1, List<String> arg2,
			List<String> arg3, String arg4) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onUnbind(Context arg0, int arg1, String arg2) {
		// TODO Auto-generated method stub

	}

	private void updateContent(Context context, String content) {

		// AppContent app=content.g
		// content
		// context.getApplication();
		AppContent app;
		app = (AppContent) context.getApplicationContext();
		Gson gson = new Gson();
		Log.v("获得的json", "" + content);
		PushMessage pushMessage = gson.fromJson(content, PushMessage.class);
		if (pushMessage == null) {
			return;
		}
		String type = pushMessage.getType();
		if (type.equals("neworder")) {
			// boolean flag=true;
			// app.SetPushMessage(flag);
			Bundle bundle = new Bundle();
			//新的订单
			bundle.putSerializable("orderMessage", pushMessage);
			bundle.putString("State", "1");
			bundle.putString("Is_play_sound", "play");
			Intent intent = new Intent();
			intent.setClass(context.getApplicationContext(), OrderDetails.class);
			intent.putExtras(bundle);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.getApplicationContext().startActivity(intent);
			app.New_Order_Push = true;
			// context.getApplicationContext().startService(intent2);
	//		app.IniOrderLastTimeReceiver(pushMessage.getOrderId());
			// app.SetPushMessage(true,pushMessage.getMessage(),STR_Content);
		}
		if (type.equals("newteam")) {
			Log.v("车队邀请", "车队邀请");
			if (app != null) {
				Log.v("车队邀请", "车队邀请");

				app.setCarteamPushMessage(true, pushMessage.getMessage(),
						pushMessage.getInvitation() + "",
						pushMessage.getMotorcadeid() + "");
			} else {
				Log.v("PushMessageReceiver", "是空");
			}

			// intent.setClass(context.getApplicationContext(),
			// MyOrderListActivity.class);
			// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			// context.getApplicationContext().startActivity(intent);
		}

		if (type.equals("deleteteam")) {
			Log.v("车队删除", "车队删除");
			if (app != null) {
				Log.v("车队邀请", "车队邀请");
				app.CarDeletMessage(true, pushMessage.getMessage(),
						pushMessage.getInvitation() + "",
						pushMessage.getMotorcadeid() + "");
				// app.SetPushMessage(true, pushMessage.getMessage(),
				// pushMessage.getInvitation() + "",
				// pushMessage.getMotorcadeid() + "");
			} else {
				Log.v("PushMessageReceiver", "是空");
			}
		}

		// 公司车队
		if (type.equals("newcompanycarteam")) {
			Log.v("车队", "公司车队");
			if (app != null) {
				app.CompleteCarTeamMessage(pushMessage.getMessage(),
						pushMessage.getOrgid() + "", "");
			} else {
				Log.v("PushMessageReceiver", "是空");
			}
		}
		/**
		 * 司机验证 carownerid: 2, type: "userCheckedDenies", message:
		 * "xxx司机,你的信息没有审核通过,原因: xxxxxxxxxx"
		 * 
		 */
		if (type.equals("userCheckedDenies")) {
			if (app != null) {
				app.ondriverVerification(pushMessage.getMessage());
			}
		}

		if (type.equals("userCheckedApprove")) {
			if (app != null) {
				app.userCheckComplete(pushMessage.getMessage());
			}
		}

	}

	private void UpdateToService(String channelId) {
		Map<String, Object> dataMap = new HashMap<String, Object>();
		// JsonReader jr=JsonRequest.getInit();
		// dataMap.put(arg0, arg1)
	}

	@Override
	public void onNotificationArrived(Context arg0, String arg1, String arg2,
			String arg3) {
		// TODO Auto-generated method stub

	}

}
